public class linklist {
    public static node head;
    static class node {
        public int data;
        public node next;
        public node(int data) {
            this.data=data;
        }
    }
    public void createList() {
        node n1=new node(1);
        this.head=n1;
    }
    public void addLast(int data) {
        node tem = new node(data);
        if(head==null) {
            head=tem;
        } else {
            node cur=head;
            while(cur.next!=null) {
                cur=cur.next;
            }
            cur.next=tem;
        }
    }
    public void addFirst(int data) {
        node tem = new node(data);
        if(head==null) {
            head=tem;
        } else {
            tem.next = head.next;
            head.next = tem;
        }
    }
    public void addIndex(int dex,int data) {
        node cur=head;
        node tem=new node(data);
        for(int i=1;i<dex;i++) {
            if(cur==null) {
                System.out.println("没有该节点");
                return;
            }
            cur=cur.next;
        }
        tem.next=cur.next;
        cur.next=tem;
    }
    public void display() {
        node cur = head;
        int i=0;
        while(cur!=null) {
            System.out.println("节点"+i+":"+cur.data);
            cur=cur.next;
            i++;
        }
    }
    public void deleteval(int val) {
        node pre=head;
        node cur=head.next;
        while(cur!=null) {
            if(cur.data==val) {
                pre.next=cur.next;
                pre=cur;
                cur=cur.next;
            } else {
                cur = cur.next;
                pre = pre.next;
            }
        }
    }
    public void delete(int dex) {
        node cur=head;
        for(int i=1;i<dex-1;i++) {
            if(cur==null) {
                System.out.println("不存在该节点");
            }
        }
        cur.next=cur.next.next;
    }
    public static void main(String[] args) {
        linklist ls = new linklist();
        ls.createList();
        ls.addFirst(2);
        ls.addLast(3);
        ls.addLast(2);
        ls.display();
        ls.addIndex(2,5);
        System.out.println("========================");
        ls.display();
        System.out.println("========================");

        ls.deleteval(2);
        ls.display();
    }
}